package com.ehotting.edsta.auth.controller;

import com.ehotting.eaf.core.exception.ServiceException;
import com.ehotting.eaf.security.service.SendSmsService;
import com.ehotting.edsta.auth.config.SmsConfig;
import com.ehotting.edsta.auth.utils.StringHelper;
import com.ehotting.eaf.core.constant.UserConstants;
import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.eaf.core.web.controller.BaseController;
import com.ehotting.eaf.core.web.domain.AjaxResult;
import com.ehotting.edsta.auth.service.impl.SysLoginService;
import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.eaf.core.domain.system.api.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/userSms")
@Api(tags = "UserSmsController", description = "c端短信相关接口")
public class UserSmsController extends BaseController {

    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SendSmsService sendSmsService;
    @Autowired
    private SmsConfig smsConfig;


    @PostMapping("/getSmsCode")
    @ApiOperation("获取手机短信验证码")
    public R<Boolean> getSmsCode(@RequestBody SysUser sysUser) {
        String smsTemplate = smsConfig.getSms_544010();
        String redisSmsCode = SmsConfig.REGISTER_SMS_CODE;

        R<Boolean> fail = sendSmsCode(sysUser, redisSmsCode, smsTemplate,true);
        if (fail != null) return fail;

        return R.ok();
    }

    @PostMapping("/sendSmsCode4Reset")
    @ApiOperation("获取手机短信验证码")
    public R<Boolean> getSmsCode4Reset(@RequestBody SysUser sysUser) {
        String smsTemplate = smsConfig.getSms_544011();
        String redisSmsCode = SmsConfig.RESET_PWD_SMS_CODE;

        R<Boolean> fail = sendSmsCode(sysUser, redisSmsCode, smsTemplate,false);
        if (fail != null) return fail;

        return R.ok();
    }

    /**
     *
     * @param sysUser 需要获取手机号等信息
     * @param redisSmsCode 缓存到Redis的key
     * @param smsTemplate 短信模板
     * @param isRegister 如果是注册短信，则需要校验手机号唯一，相同手机号只能注册一次
     * @return
     */
    private R<Boolean> sendSmsCode(SysUser sysUser, String redisSmsCode, String smsTemplate, boolean isRegister) {
        String phonenumber = sysUser.getPhonenumber();
        String existSmsCode = redisService.get(redisSmsCode + phonenumber);
        logger.info("exits:"+existSmsCode);
        if(StringUtils.hasText(existSmsCode)){
            logger.info("sms code exist");
            return R.fail("已经请求过短信认证，请120秒再请求");
        }

        String code = StringHelper.getRandomNumber(6);

        logger.info("randomCode:"+code);

        Integer smsCodeTimeout = getUserSmsCodeTimeOut();

        Map<String, String> map = new HashMap<>();
        map.put("1", code);
        map.put("2", String.valueOf(smsCodeTimeout));

        boolean b = sendSmsService.sendSMS(smsTemplate, map, phonenumber);

        if(!b){
            return R.fail("短信发送失败");
        }

        this.logger.info("sms send successful.");
        redisService.setEx(redisSmsCode + phonenumber, code,smsCodeTimeout*60, TimeUnit.SECONDS);
        return null;
    }


    protected Integer getUserSmsCodeTimeOut(){
        return 2;
    }



}
